package com.demo.search.webapp.servlet;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.apache.lucene.xmlparser.CorePlusExtensionsParser;
import org.apache.lucene.xmlparser.QueryTemplateManager;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.demo.common.ClassLoaderUtils;
import com.demo.common.StringUtils;
import com.demo.search.engine.SearchEngine;
import com.demo.search.util.LuceneUtils;
import com.demo.search.xmlparser.QueryTemplateManagerWrapper;

/**
 * 以XML文件作为From提交的Lucene查询Servlet
 * FormBaseXmlQueryServlet对查询进行封装,为各应用提供查询结果。
 * 将HttpServlet与查询结果 {@link org.apache.lucene.document.Document}进行封装。
 * @author ryuu.kk
 *
 */
public class FormBaseXmlQueryServlet extends HttpServlet {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = 3987537162774139494L;
	
	/**
	 * 日志
	 */
	protected static final Logger LOG = Logger.getLogger(FormBaseXmlQueryServlet.class);
	
	/**
	 * 检索模板名称key
	 */
	public static final String QUERY_TYPE = "_template";
	
	/**
	 * xsl文件夹关键字
	 */
	public static final String XSL_FILE_PATH = "xslFilePath";
	
	/**
	 * 索引文件关键字
	 */
	public static final String INDEX_PATH = "indexPath";
	
	/**
	 * Lucene's IndexWriter关键key
	 */
	public static final String LUCENE_INDEX_WRITER = "Lucene.IndexWriter";
	
	
	/**
	 * 查询模板管理器,将xsl文件转化为查询模板
	 */
	private QueryTemplateManager queryTemplateManager;
	
	/**
	 * Lucene的Core包扩展xml表达式分析器
	 * 允许自定义xml标签;以后将扩展该对象
	 */
	private CorePlusExtensionsParser xmlParser;
	
	/**
	 * 索引写入类
	 */
	private IndexWriter writer;
	
	/**
	 * 索引查询器
	 */
	private IndexSearcher searcher;
	
	// 使用Lucene3.6版本的分析器
	//private Analyzer analyzer = new StandardAnalyzer(org.apache.lucene.util.Version.LUCENE_36);
	private Analyzer analyzer = LuceneUtils.createChineseAnalyzer();
	
	private SearchEngine searchEngine;
	
	/**
	 * Spring容器上下文
	 */
	private ApplicationContext applicationContext;

	/**
	 * Servlet初始化
	 * <ul>
	 * 	<li>初始化配置信息</li>
	 *  <li>打开Lucene索引</li>
	 * </ul>
	 */
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		// 获得spring 上下文
		applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		queryTemplateManager = applicationContext.getBean("xmlQueryTemplateManager", QueryTemplateManager.class);
		searchEngine =  applicationContext.getBean("annotationEngine", SearchEngine.class);
		try {
			// 创建样例Demo的索引
			// openExampleIndex(config);
			// 打开索引
			openIndex(config);

			// 取得xsl所在文件夹
			String xslFilePath = config.getInitParameter(XSL_FILE_PATH);
			// 取得默认field名
			String defaultStandardQueryParserField = config.getInitParameter("defaultStandardQueryParserField");

			// Load and cache choice of XSL query template using
			// QueryTemplateManager
			
			// queryTemplateManager = new QueryTemplateManager(getServletContext().getResourceAsStream("/WEB-INF/" + xslFilePath));
			if (queryTemplateManager == null) {
				queryTemplateManager = new QueryTemplateManagerWrapper(getServletContext().getRealPath("/WEB-INF/" + xslFilePath), new java.io.FileFilter(){
					// 只接受xsl文件
					@Override
					public boolean accept(File file) {
						return (file.exists() && file.getPath().endsWith(".xsl"));
					}
				}) {
					@Override
					public String getRealPath(String path) {
						return ClassLoaderUtils.getRealPath(path);
					}
				};
			}
			// initialize an XML Query Parser for use by all threads
			xmlParser = new CorePlusExtensionsParser(defaultStandardQueryParserField, analyzer);
		} catch (Exception e) {
			throw new ServletException("Error loading query template", e);
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		// Take all completed form fields and add to a Properties object
		Properties completedFormFields = new Properties();
		Enumeration<?> pNames = request.getParameterNames();
		while (pNames.hasMoreElements()) {
			String propName = (String) pNames.nextElement();
			String value = request.getParameter(propName);
			if ((value != null) && (value.trim().length() > 0)) {
				completedFormFields.setProperty(propName, value);
			}
		}

		org.w3c.dom.Document xmlQuery = null;
		// 取得模板名称
		String templateName = request.getParameter(QUERY_TYPE);
		try {
			if (StringUtils.isEmpty(templateName)) {
				// 空模板名称,则使用默认模板
				xmlQuery = queryTemplateManager.getQueryAsDOM(completedFormFields);
			} else {
				// Create an XML query by populating template with given user
				// criteria
				xmlQuery = queryTemplateManager.getQueryAsDOM(completedFormFields, templateName);
			}

			// Parse the XML to produce a Lucene query
			Query query = xmlParser.getQuery(xmlQuery.getDocumentElement());

			// Run the query
			TopDocs topDocs = searcher.search(query, 10);
			// and package the results and forward to JSP
			if (topDocs != null) {
				ScoreDoc[] sd = topDocs.scoreDocs;
				Document[] results = new Document[sd.length];
				//searchEngine.documentToTarget(results, clazz);
				for (int i = 0; i < results.length; i++) {
					results[i] = searcher.doc(sd[i].doc);
					
					request.setAttribute("results", results);
					
				}
			}
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index3.jsp");
			dispatcher.forward(request, response);
		} catch (java.lang.NullPointerException e) {
			throw new ServletException("not found query template [" + templateName + "]", e);
		}catch (Exception e) {
			throw new ServletException("Error processing query", e);
		}
	}

	private void openExampleIndex(ServletConfig config) throws CorruptIndexException, IOException {

		// Create a RAM-based index from our test data file
		//RAMDirectory rd = new RAMDirectory();
		//IndexWriter writer = new IndexWriter(rd, analyzer, IndexWriter.MaxFieldLength.LIMITED);
		writer = getWriter(config);
		InputStream dataIn = getServletContext().getResourceAsStream("/WEB-INF/search/index/data.tsv");
		BufferedReader br = new BufferedReader(new InputStreamReader(dataIn));
		String line = br.readLine();
		while (line != null) {
			line = line.trim();
			if (line.length() > 0) {
				// parse row and create a document
				StringTokenizer st = new StringTokenizer(line, "\t");
				Document doc = new Document();
				doc.add(new Field("location", st.nextToken(), Field.Store.YES,
						Field.Index.ANALYZED_NO_NORMS));
				doc.add(new Field("salary", st.nextToken(), Field.Store.YES,
						Field.Index.ANALYZED_NO_NORMS));
				doc.add(new Field("type", st.nextToken(), Field.Store.YES,
						Field.Index.ANALYZED_NO_NORMS));
				doc.add(new Field("description", st.nextToken(),
						Field.Store.YES, Field.Index.ANALYZED));
				writer.addDocument(doc);
			}
			line = br.readLine();
		}
		writer.commit();
		//writer.close();
		// 获得一个近实时索引(a near real time IndexReader)
		IndexReader indexReader = IndexReader.open(writer, true);
		
		// open searcher
		searcher = new IndexSearcher(indexReader);
	}
	
	/**
	 * 获得Lucene的索引(近实时速印)
	 * @param config ServletConfig
	 * @throws CorruptIndexException
	 * @throws IOException
	 */
	protected void openIndex(ServletConfig config) throws CorruptIndexException, IOException {
		// 获得IndexWriter
		IndexWriter writer = getWriter(config);
		// 获得一个近实时索引(a near real time IndexReader)
		IndexReader indexReader = IndexReader.open(writer, true);
		
		// 索引快照
		IndexReader newReader = IndexReader.openIfChanged(indexReader);
		if (newReader != null) {
			indexReader.close();
			indexReader = newReader;
		}
		this.searcher = new IndexSearcher(indexReader);
	}
	
	/**
	 * 获得一个Lucene的写索引对象
	 * @param config ServletConfig
	 * @return IndexWriter
	 * @throws IOException
	 */
	protected IndexWriter getWriter(ServletConfig config) throws IOException {
		
		if (writer != null) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("Get IndexWriter [" + writer.toString() + "] from FormBaseXmlQueryServlet.");
			}
			return writer;
		}
		// 得到ServletContext中的IndexWriter
		writer = (IndexWriter) config.getServletContext().getAttribute(LUCENE_INDEX_WRITER);
		if (writer != null) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("Get IndexWriter [" + writer.toString() + "] from ServletContext.");
			}
			return writer;
		}
		writer = this.applicationContext.getBean("indexWriter", IndexWriter.class);
		if (writer != null) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("Get IndexWriter [" + writer.toString() + "] from ApplicationContext.");
			}
			return writer;
		}
		
		// 创建IndexWriter
		String indexPath = getInitParameter(INDEX_PATH);
		indexPath = getServletContext().getRealPath("/WEB-INF/" + indexPath);
		
		Directory directory = LuceneUtils.createFSDriectory(indexPath);
		IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer);
		try {
			writer = new IndexWriter(directory, writerConfig);
		} catch (LockObtainFailedException e) {
			//writerConfig.getDefaultWriteLockTimeout();
			e.printStackTrace();
		}
		// 设置Lucene的IndexWriter,目的是近实时查询
		config.getServletContext().setAttribute(LUCENE_INDEX_WRITER, writer);
		if (LOG.isDebugEnabled()) {
			LOG.debug("IndexWriter no existed, Create it: [" + writer.toString() + "]");
		}
		return writer;
	}

	public IndexWriter getWriter() {
		return writer;
	}

	public IndexSearcher getSearcher() {
		return searcher;
	}

	public void setAnalyzer(Analyzer analyzer) {
		this.analyzer = analyzer;
	}
	
	public Analyzer getAnalyzer() {
		return analyzer;
	}
}
